package core

type Message interface {
	GetId() int64

	GetKey() string
	SetKey(key string)

	GetPayload() []byte
	SetPayload(payload []byte)

	GetArgs(key string) string

	PutContext(key string, value interface{})
	GetContext(key string) (interface{}, bool)
}

type message struct {
	id      int64
	key     string
	payload []byte
	args    map[string]string
	context map[string]interface{}
}

func NewMessage() Message {
	return &message{
		id:      SnowflakeId.NextId(),
		payload: make([]byte, 0),
		args:    make(map[string]string),
		context: make(map[string]interface{})}
}

func (this *message) GetId() int64 {
	return this.id
}

func (this *message) GetKey() string {
	return this.key
}

func (this *message) SetKey(key string) {
	this.key = key
}

func (this *message) GetPayload() []byte {
	p := make([]byte, len(this.payload))
	copy(p, this.payload)
	return p
}

func (this *message) SetPayload(payload []byte) {
	len := len(payload)
	if len <= 0 {
		return
	}
	this.payload = make([]byte, len)
	copy(this.payload, payload)
}

func (this *message) GetArgs(key string) string {
	return this.args[key]
}

func (this *message) PutContext(key string, value interface{}) {
	this.context[key] = value
}

func (this *message) GetContext(key string) (interface{}, bool) {
	v, ok := this.context[key]
	return v, ok
}
